%% parameters
[filename_part pathname] = uigetfile('*.*','Please select a fid file');
filename = [pathname filename_part]
c_arg = {2}; % display nothing.
channel = 4;

%% Make data mat
[~, headerinfo] = readfidn(filename, 1, 1, c_arg);

nblocks=headerinfo{1, 1}(1); % header info
ntraces=headerinfo{1, 1}(2); % status info
dataP=headerinfo{1, 1}(3)/2; 

complexFID=cell(nblocks, ntraces);

for ix_trace=1:1:ntraces
    for ix_block=1:1:nblocks
        [complexFID{ix_block, ix_trace}, ~] = readfidn(filename, ix_block, ix_trace, c_arg);
    end
end

%% FFT 
cmplxSpec = cell(nblocks, ntraces);
for ix_trace=1:1:ntraces
    for ix_block=1:1:nblocks
        cmplxSpec{ix_block, ix_trace} = fft(complexFID{ix_block, ix_trace});
    end
end

%% Display
for ix_trace=1:1:1
    for ix_block=1:1:4
        figure(1);
        subplot(4, 1, ix_block+ix_trace-1);
        plot(real(complexFID{ix_block, ix_trace}));
        figure(2);
        subplot(4, 1, ix_block+ix_trace-1);
        plot(circshift(real(cmplxSpec{ix_block, ix_trace}), dataP/2));
    end
end

%% substraction 
% sub_data=complexFID{1,1} - complexFID{6,4};
% figure(2);
% plot(real(sub_data));


%% each test
% [c_realFID1, c_imagFID1] = readfidn(filename, 2, 4, {4, 2});
% [c_realFID2, c_imagFID2] = readfidn(filename, 32, 1, {4, 2});
% 
% figure(3);
% plot(c_realFID1-c_realFID2);
% max(c_realFID1-c_realFID2)


